import ru.niir.goodfil.db.GoodfilFactory
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import ru.niir.goodfil.db.Client

baseDir = "C:\\Documents and Settings\\Admin\\Рабочий стол";

gf = GoodfilFactory.instance;

wb = new HSSFWorkbook(new FileInputStream("${baseDir}\\clients_real.xls"));
sheet = wb.getSheetAt(0);

columns = [:]
row0 = sheet.getRow(0);
for (i = 0; i < row0.lastCellNum; i++)
{
  cell = row0.getCell(i);
  columns[cell.stringCellValue] = i;
}

companyNamesSet = [:];
admin_mail = gf.get("admin.mail");
clients = [];
for (i = 1; i <= sheet.lastRowNum; i++)
{
  row = sheet.getRow(i);
  client = new Client();
  client.c1ClientNum      = row.getCell(columns['id1C']).numericCellValue.toString();
  client.companyName      = row.getCell(columns['companyName']).stringCellValue;
  client.lowName          = row.getCell(columns['lawName']).stringCellValue;
  client.addressLaw       = row.getCell(columns['addressLaw']).stringCellValue;
  client.addressFact      = row.getCell(columns['addressFact']).stringCellValue;
  client.inn              = row.getCell(columns['Inn']).stringCellValue;
  client.okpo             = row.getCell(columns['Okpo']).stringCellValue;
  client.bank             = row.getCell(columns['Bank']).stringCellValue;
  client.bik              = row.getCell(columns['Bik']).stringCellValue;
  client.accountCor       = row.getCell(columns['AccountCor']).stringCellValue;
  client.account          = row.getCell(columns['Account']).stringCellValue;
  client.comment          = row.getCell(columns['Komment']).stringCellValue;
  client.contactFio       = row.getCell(columns['contaktFio']).stringCellValue;
  client.contactEmail     = admin_mail;
  client.clientGroupId    = gf.mainClientGroupId;

  if (!companyNamesSet.containsKey(client.companyName.toLowerCase()))
    companyNamesSet[client.companyName.toLowerCase()] = 1;
  companyNamesSet[client.companyName.toLowerCase()] = companyNamesSet[client.companyName.toLowerCase()] + 1;

  clients.add(client);
}

count = 0;
for (companyName in companyNamesSet.keySet())
{
  if (companyNamesSet[companyName] > 2)
  {
    println("! ${companyName}");
    count++;
  }
}

println("!!! ${count}");

if (count > 0) System.exit(0);

println "clients count: ${clients.size()}"
for(client in clients)
{
  vresult = gf.validate(client);
  if (vresult.isError())
  {
    println client.toClientString();
    println vresult.toHumanString();
    println ("---");
  }
}

clients.each {client -> gf.saveClient(client) }